package com.travel.server.manager.impl.dal.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.OrderInfoDao;
import com.travel.server.manager.dao.ProjectConsultDao;
import com.travel.server.manager.dao.ProjectInfoDao;
import com.travel.server.manager.impl.enums.OrderStateEnum;
import com.travel.server.manager.impl.enums.OrderStepEnum;
import com.travel.server.manager.model.OrderInfoExample;
import com.travel.server.manager.model.ProjectConsult;
import com.travel.server.manager.model.ProjectConsultExample;
import com.travel.server.manager.model.ProjectInfoExample;
import com.travel.server.manager.model.dto.data.ProjectOrderDto;
import com.travel.server.manager.model.dto.data.ProjectTrendDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/30 10:12
 * @Desc:
 */
@Component
public class ProjectDataDal {

    @Autowired
    private ProjectInfoDao projectInfoDao;

    @Autowired
    private OrderInfoDao orderInfoDao;

    public final static List<String> APPLY_ARRAY = Arrays.asList(new String[]{OrderStepEnum.PROJECT_WAIT.getStep(), OrderStepEnum.ORDER_WAIT.getStep(), OrderStepEnum.QUESTION_WAIT_SUBMIT.getStep(),
            OrderStepEnum.QUESTION_WAIT_EXAM.getStep(), OrderStepEnum.DATA_WAIT_SUBMIT.getStep(), OrderStepEnum.DATA_WAIT_EXAM.getStep(), OrderStepEnum.ING.getStep(),
            OrderStepEnum.END.getStep(), OrderStepEnum.ORDER_REFUND.getStep(), OrderStepEnum.ORDER_CLOSE.getStep(), OrderStepEnum.WAIT_CONTRACT.getStep()});

    public final static List<String> ORDER_ARRAY =  Arrays.asList(new String[]{OrderStepEnum.ORDER_WAIT.getStep(), OrderStepEnum.QUESTION_WAIT_SUBMIT.getStep(),
            OrderStepEnum.QUESTION_WAIT_EXAM.getStep(), OrderStepEnum.DATA_WAIT_SUBMIT.getStep(), OrderStepEnum.DATA_WAIT_EXAM.getStep(), OrderStepEnum.ING.getStep(),
            OrderStepEnum.END.getStep(), OrderStepEnum.ORDER_REFUND.getStep(), OrderStepEnum.ORDER_CLOSE.getStep(), OrderStepEnum.WAIT_CONTRACT.getStep()});

    public Integer selectShelf(){
        return projectInfoDao.selectShelf();
    }

    public Integer selectOrder(Date startTime, Date endTime){
        OrderInfoExample example = new OrderInfoExample();
        OrderInfoExample.Criteria criteria = example.or();
        criteria.andCreateTimeBetween(startTime, endTime);
        return orderInfoDao.countByExample(example);
    }

    public Integer selectPay(Date startTime, Date endTime){
        OrderInfoExample example = new OrderInfoExample();
        OrderInfoExample.Criteria criteria = example.or();
        criteria.andCreateTimeBetween(startTime, endTime);
        criteria.andOrderStateNotEqualTo(OrderStateEnum.WAIT_PAY.getState());
        return orderInfoDao.countByExample(example);
    }

    public List<ProjectOrderDto> selectProjectOrder(Date startTime, Date endTime, Integer country, String projectName, Integer offset, Integer limit){
        if(StringUtils.isNotBlank(projectName)){
            projectName = "%" + projectName + "%";
        }
        return projectInfoDao.selectProjectOrder(startTime, endTime, country, projectName, 1, offset, limit);
    }

    public Integer selectProjectOrderCount(Integer country, String projectName){
        ProjectInfoExample example = new ProjectInfoExample();
        ProjectInfoExample.Criteria criteria = example.or();
        if(CommonConstant.YES.equals(country)){
            criteria.andCountryEqualTo("中国");
        }else if(new Integer(2).equals(country)){
            criteria.andCountryNotEqualTo("中国");
        }
        if(StringUtils.isNotBlank(projectName)){
            criteria.andNameLike("%" + projectName + "%");
        }
        return projectInfoDao.countByExample(example);
    }

    public List<ProjectTrendDto> selectProjectTrend(Date startTime, Date endTime, List<Integer> specialIdList, List<String> orderStepList){
        return projectInfoDao.selectProjectTrend(startTime,endTime, specialIdList, orderStepList, CommonConstant.YES);
    }
}
